package cn.example.androidthreadtest;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telecom.Connection;
import android.telecom.ConnectionService;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final int UPDATE_TEXT=1;

    private Button button;
    private TextView textView;

    //services
    private Button btn_start;
    private Button btn_stop;
    private Button btn_bindstart;
    private Button btn_unbindstart;
    private Button btn_forgerservices;

    private MyService2.DownloadBinder downloadBinder;

    //连接管理器
   private ServiceConnection serviceConnection=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downloadBinder=(MyService2.DownloadBinder)iBinder;
            //指定服务去干什么
            downloadBinder.startDownload();
            downloadBinder.getProgress();

        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button=findViewById(R.id.button);
        textView=findViewById(R.id.textView);
        button.setOnClickListener(this);
        //
        btn_start=findViewById(R.id.btn_start);
        btn_stop=findViewById(R.id.btn_stop);
        btn_bindstart=findViewById(R.id.btn_bindstart);
        btn_unbindstart=findViewById(R.id.btn_unbindstart);
        btn_forgerservices=findViewById(R.id.btn_forgerservices);
        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        btn_bindstart.setOnClickListener(this);
        btn_unbindstart.setOnClickListener(this);
        btn_forgerservices.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                changeTextContent();
                break;
            case R.id.btn_start://启动服务
                Intent startIntent  = new Intent(this, MyService.class);
                startService(startIntent );
                break;
            case R.id.btn_stop://停止服务
                Intent stopIntent   = new Intent(this, MyService.class);
                stopService(stopIntent);
                break;
            case R.id.btn_bindstart://启动服务
                Intent startbindIntent   = new Intent(this, MyService2.class);
                bindService(startbindIntent,serviceConnection, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btn_unbindstart://停止服务
                unbindService(serviceConnection);
                break;
            case R.id.btn_forgerservices://开启通知服务
                startService(new Intent(this,MyService3.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(serviceConnection);
    }

    //一步消息处理
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case UPDATE_TEXT:
                    textView.setText("sss");
                    break;
            }

        }
    };
    private void changeTextContent() {
        //出错
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                textView.setText("sss");
            }
        }).start();*/
        //正确
       /* runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText("sss");
            }
        });*/
       //异步消息处理
        new Thread(new Runnable() {
            @Override
            public void run() {
                Message message = new Message();
                message.what=UPDATE_TEXT;
                handler.sendMessage(message);

            }
        }).start();
    }
}
